home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9062 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: apccorp.apcc.com!root
  2. From: nfegan@apcc.com (Noel Fegan)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help with bizzare bug (Long)
  5. Date: Wed, 28 Feb 1996 10:04:00 GMT
  6. Organization: American Power Conversion
  7. Message-ID: <4h19bq$m3m@apccorp.apcc.com>
  8. References: <tday-2702962256130001@tday.slip.netcom.com>
  9. NNTP-Posting-Host: hewie.galway.apcc.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. tday@netcom.com (Tony Day) wrote:
  13.  
  14. >>NameAr & NameAr::operator=(const NameAr & a)
  15. >>{
  16. >>    if (this == &a)      // if object assigned to self,
  17. >>      return *this;  // don't change anything
  18. >>   ArrayDb::operator=(a);
  19. >>   delete [] name;
  20. >>   name = new char[strlen(a.name)+1];
  21. >>   strcpy(name, a.name);
  22. >>   return *this;
  23. >>}
  24.  
  25. >and the offending statement is "delete [] name" (if it is removed the
  26. >program runs as wriiten)
  27.  
  28. >   
  29. >NameAr::NameAr()
  30. >{ 
  31. >   name = new char[strlen("Smiley")+1];
  32. >   name = "Smiley";
  33. >}     
  34.  
  35. >
  36. >NameAr::NameAr(const ArrayDb & a): ArrayDb(a)
  37. >{
  38. >   name = new char[strlen("Smiley")+1];
  39. >   name = "Smiley";
  40.  
  41. >}     
  42.  
  43. The two constructors above set "name" to point to the character string "Smiley"
  44. in both cases. However, this did no involve newing memory and copying the string
  45. into the new'ed block. So when the NameAr::operator= function is called it tries
  46. to delete the character array "Smiley" which it has no business doing because
  47. the block was not newed. The destructor of NameAr would also have a problem.
  48. --
  49. Noel Fegan
  50. American Power Conversion
  51.  
  52.